﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using TA.DotNetUtils;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Collection of controls which in addition to serving as a storage for control collection, also adds the controls to a parent control's children collection.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class ControlCollection<T> : VList<T>
		where T : Control
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		/// <param name="owner">The parent control to be used as the container for added controls.</param>
		public ControlCollection(Control owner)
		{
			this._owner = owner;
		}
		#endregion
		#region PROPS
		/// <summary>
		/// Gets the control which owns this collection.
		/// </summary>
		public Control Owner
		{
			get
			{
				return this._owner;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region VARS
		Control _owner;
		#endregion
		#region METHODS
		/// <summary>
		/// Called when a control is added.
		/// </summary>
		/// <param name="item"></param>
		protected override void OnItemAdded(T item)
		{
			base.OnItemAdded(item);
			this._owner.Controls.Add(item);
		}
		/// <summary>
		/// Called when a control is removed.
		/// </summary>
		protected override void OnItemRemoved(T item)
		{
			base.OnItemRemoved(item);

			this._owner.Controls.Remove(item);
		}
		#endregion
		#endregion
	}
}
